package cn.yl.fxboot.application.controller;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Autowired;

import cn.hutool.core.date.DateUtil;
import cn.yl.fxboot.application.action.AuthorActions;
import cn.yl.fxboot.application.action.MsgPaneActions;
import cn.yl.fxboot.application.action.MultiActions;
import cn.yl.fxboot.application.action.SingleActions;
import cn.yl.fxboot.application.action.MixAction;
import cn.yl.fxboot.application.action.model.AuthorStartActModel;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebView;

/**
 * 
 * @author Yal
 *
 */
@FXMLController
public class MainStageController implements Initializable{
	
	
	/*********singleTab *******/
	@FXML
	private Tab singleTab;
	@FXML
	private Button singleAddr_Btn;
	@FXML
	private TextField singleAddr_TF;
	@FXML
	private TextArea singleUrl_Area;
	@FXML
	private Button singleStart_Btn;
	
	
	/*********multiPartTab *******/
	@FXML
	private Tab multiTab;
	@FXML
	private Button multiSrc_Btn;
	@FXML
	private Button multiAddr_Btn;
//	@FXML
//	private Button multiSaveSetting_Btn;
	@FXML
	private Button multiStart_Btn;
	@FXML
	private TextField multiSrc_TF;
	@FXML
	private TextField multiAddr_TF;
	@FXML
	private CheckBox multiTitle_CB;
	@FXML
	private CheckBox multiAuthor_CB;
	@FXML
	private CheckBox multiDynamicCover_CB;
	@FXML
	private CheckBox multiCover_CB;
	@FXML
	private CheckBox multiSrcUrl_CB;
	@FXML
	private CheckBox multiVideo_CB;
	@FXML
	private CheckBox multiMd5_CB;
	
	
	/*********authorTab *******/
	@FXML
	private Button authorAddr_Btn;
	@FXML
	private TextField authorAddr_TF;
	@FXML
	private TextArea authorUrl_TA;
	@FXML
	private Button authorStart_Btn;
	@FXML
	private WebView authorView_WV;
	@FXML
	private Button clear_Btn;

	
	/*********YoutubeTab *******/
	@FXML
	public Button mixStart_Btn;
	@FXML
	public Button mixSrc_Btn;
	@FXML
	public Button mixAddr_Btn;
	@FXML
	public TextField mixSrc_TF;
	@FXML
	public TextField mixAddr_TF;
	@FXML
	public TextField mixCount_TF;
	@FXML
	public TextField mixVideoHead_TF;
	@FXML
	public TextField mixVideoEnd_TF;
	@FXML
	public Button mixVideoHead_Btn;
	@FXML
	public Button mixVideoEnd_Btn;
	
	@FXML
	public TextField FFMPEG_TF;
	@FXML
	public Button FFMPEG_Btn;
	
	

	
	/**********MSG PANE*******/
	@FXML
	private AnchorPane msgPane;
	@FXML
	private TextField msgTpl_TF;
	@FXML
	private ScrollPane logScrollPane;
	
	private Tab currentTab;
	
	@Autowired
	Executor customServiceExecutor;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		currentTab = singleTab;
		initMsgPane();
		initSingleTab();
		initMultiTab();
		initAuthorTab();
		initSettingTab();
		initMixTab();
		MsgPaneActions.init(msgPane, msgTpl_TF);
	}

	private void initSingleTab() {
		SingleActions actions = new SingleActions();
		singleAddr_Btn.setOnAction(actions.singleAddrBtnHandler(singleAddr_TF));
		singleStart_Btn.setOnAction(actions.singleStartBtnHandler(singleUrl_Area,singleAddr_TF));
	}
	
	private void initMultiTab() {
		MultiActions actions = new MultiActions();
		multiSrc_Btn.setOnAction(actions.multiSrcBtnHandler(multiSrc_TF));
		multiAddr_Btn.setOnAction(actions.multiAddrBtnHandler(multiAddr_TF));
		multiStart_Btn.setOnAction(actions.multiDownloadHandler(multiSrc_TF,multiAddr_TF));
	}

	private void initAuthorTab() {
		//初始化dy环境
		authorView_WV.getEngine().load("https://v.douyin.com/t6xFbV/");
		AuthorActions authorAction = new AuthorActions();
		authorAddr_Btn.setOnAction(authorAction.authorAddrBtnHandler(authorAddr_TF));
		clear_Btn.setOnAction(authorAction.clearBtnHandler(clear_Btn));
		AuthorStartActModel startModel = new AuthorStartActModel();
		startModel.setAuthorAddrTF(authorAddr_TF);
		startModel.setAuthorUrlTA(authorUrl_TA);
		startModel.setAuthorViewWV(authorView_WV);
		authorStart_Btn.setOnAction(authorAction.authorDownHandler(startModel));
	}
	
	private void initMixTab() {
		MixAction action = new MixAction();
		mixAddr_Btn.setOnAction(action.mixAddrBtnHandler(mixAddr_TF));
		mixSrc_Btn.setOnAction(action.mixSrcBtnHandler(mixSrc_TF));
		mixStart_Btn.setOnAction(action.startHandler(mixAddr_TF,mixSrc_TF));
		mixVideoHead_Btn.setOnAction(action.mixVideoHeadBtnHandler(mixVideoHead_TF));
		mixVideoEnd_Btn.setOnAction(action.mixVideoEndBtnHandler(mixVideoEnd_TF));
		FFMPEG_Btn.setOnAction(action.FFMPEGBtnHandler(FFMPEG_TF));
	}

	private void initSettingTab() {
		// TODO Auto-generated method stub
	}
	
	private void initMsgPane() {
		msgTpl_TF.setText(DateUtil.now() + " "+"  欢迎回来");
	}

	public AnchorPane getMsgPane() {
		return msgPane;
	}

	public TextField getMsgTpl_TF() {
		return msgTpl_TF;
	}

}
